package com.yumi.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yumi.entity.TrainingPlan;
import com.yumi.mapper.TrainingPlanMapper;
import com.yumi.service.TrainingPlanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * 培训计划服务实现类
 */
@Slf4j
@Service
public class TrainingPlanServiceImpl implements TrainingPlanService {

    @Autowired
    private TrainingPlanMapper trainingPlanMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TrainingPlan createTrainingPlan(TrainingPlan trainingPlan) {
        // 设置初始状态
        if (trainingPlan.getStatus() == null) {
            trainingPlan.setStatus(0); // 默认为未开始状态
        }
        
        // 设置最大参与人数（如果未设置）
        if (trainingPlan.getMaxParticipants() == null) {
            trainingPlan.setMaxParticipants(50); // 默认最大参与人数为50
        }
        
        // 保存培训计划
        trainingPlanMapper.insert(trainingPlan);
        
        // 返回创建的培训计划（包含自动生成的ID）
        return trainingPlanMapper.selectById(trainingPlan.getId());
    }

    @Override
    public Page<TrainingPlan> pageQuery(int page, int pageSize, String title, Integer type, Integer status) {
        Page<TrainingPlan> pageInfo = new Page<>(page, pageSize);
        
        LambdaQueryWrapper<TrainingPlan> queryWrapper = new LambdaQueryWrapper<>();
        
        // 添加标题搜索条件
        if (StringUtils.hasText(title)) {
            queryWrapper.like(TrainingPlan::getTitle, title);
        }
        
        // 添加类型筛选条件
        if (type != null) {
            queryWrapper.eq(TrainingPlan::getType, type);
        }
        
        // 添加状态筛选条件
        if (status != null) {
            queryWrapper.eq(TrainingPlan::getStatus, status);
        }
        
        // 按创建时间降序排序
        queryWrapper.orderByDesc(TrainingPlan::getCreateTime);
        
        return trainingPlanMapper.selectPage(pageInfo, queryWrapper);
    }

    @Override
    public TrainingPlan getTrainingPlanById(Long id) {
        return trainingPlanMapper.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TrainingPlan updateTrainingPlan(Long id, TrainingPlan trainingPlan) {
        // 检查培训计划是否存在
        TrainingPlan existPlan = trainingPlanMapper.selectById(id);
        if (existPlan == null) {
            throw new RuntimeException("培训计划不存在");
        }
        
        // 设置ID，防止前端传入的ID与路径ID不一致
        trainingPlan.setId(id);
        
        // 更新培训计划
        trainingPlanMapper.updateById(trainingPlan);
        
        // 返回更新后的培训计划
        return trainingPlanMapper.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteTrainingPlan(Long id) {
        // 检查培训计划是否存在
        TrainingPlan trainingPlan = trainingPlanMapper.selectById(id);
        if (trainingPlan == null) {
            throw new RuntimeException("培训计划不存在");
        }
        
        // 删除培训计划（MyBatis-Plus已配置逻辑删除，会自动修改deleted字段）
        trainingPlanMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TrainingPlan updateTrainingPlanStatus(Long id, Integer status) {
        // 检查培训计划是否存在
        TrainingPlan trainingPlan = trainingPlanMapper.selectById(id);
        if (trainingPlan == null) {
            throw new RuntimeException("培训计划不存在");
        }
        
        // 检查状态值是否合法
        if (status < 0 || status > 2) {
            throw new RuntimeException("培训状态值不合法");
        }
        
        // 更新状态
        trainingPlan.setStatus(status);
        trainingPlanMapper.updateById(trainingPlan);
        
        // 返回更新后的培训计划
        return trainingPlanMapper.selectById(id);
    }
} 